<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/5/18
 * Time: 15:55
 * 用户管理模块
 */
class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('user_model');
        $this->load->model('role_model');
    }

    //登录首页
    //2017/5/18 14:12:01
//    public function index()
//    {
//        $this->load->view('home');
//    }

    /**
     * 用户列表,判断search是否存在进行模糊搜索
     */
    public function index(){

        //只有管理员权限才能访问
        if($_COOKIE['username'] !== 'admin'){
            die('抱歉你没权限访问此页面');
        }
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'username',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->user_model->search('*',0,$searchArg,$page_num,$start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('admin/user/list',$viewData);
        } else {
            $data = $this->user_model->page('user',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            $this->load->view('admin/user/list',$viewData);
        }
    }

    //2017-5-21 11:56:01  数据库添加完成
    //添加功能
    public function add(){

        $data = [
            'username' => $this->input->post('username'),
            'nickname' => $this->input->post('nickname'),
            'password' => $this->input->post('password'),
            'loginTime' => time(),
        ];

        //是否会有添加失败的情况?

        $resultData =  $this->user_model->add($data);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/user/list');
        }
    }

    //添加页面
    public function addView(){

        $this->load->view('/admin/user/add');
    }

    //编辑功能
    public function edit(){
        //用户id
        $where = [
            'id' => $this->input->post('id')
        ];
        //todo 更多字段
        $data = [
            'nickname' => $this->input->post('nickname'),
        ];

        //是否会有添加失败的情况?
        $resultData =  $this->user_model->edit($data,$where);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/user/index');
        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $result = $this->user_model->getByFieldQueryData('id,username',$arg,0);
        $data = [
            'id' => $result['id'],
            'username' => $result['username']
        ];

        $this->load->view('/admin/user/edit',$data);
    }


    //批量删除
    public function remove(){

        $postData = $this->input->post();

        if($postData){
            if(!$postData){
                redirect('/admin/user/index');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->user_model->remove($where);
            }
            redirect('/admin/user/index');

        }else{
            $data = [
                'id' => $this->input->get('id'),
            ];

            //是否会有添加失败的情况?
            $resultData =  $this->user_model->remove($data);
            if($resultData){
                redirect('/admin/user/list');
            }
        }

    }

    //2017-5-20 15:35:15
    public function login()
    {
        if ($this->input->post()) {

            //region CI自带验证
            $config = array(
                array(
                    'field' => 'username',
                    'label' => '用户名',
                    'rules' => 'trim|required',
                ),
                array(
                    'field' => 'password',
                    'label' => '密码',
                    'rules' => 'trim|required',
                )
            );

            $this->form_validation->set_rules($config);
            $arg = [
                'username' =>$this->input->post('username'),
                'password' =>$this->input->post('password'),
            ];
            //查询username 查询用户,密码
            $resultPass = $this->user_model->getByFieldQueryData('username,password',$arg,0);

            //判断是否为空,空则说明对了,不空则说明错误
            if((!$resultPass) || (array_diff($arg,$resultPass)) || $this->form_validation->run() == FALSE){
                die(__alert('请检查用户名或密码222'));
            }

            set_cookie('username',$resultPass['username'],9999899999);
//            $resultPass = $this->user_model->getByFieldQueryData('username,password',$arg,0);

            //每次登录更新用户登录时间
            $where = [
                'username' => $this->input->post('username')
            ];
            $loginTime = [
                'loginTime' => time()
            ];
            $this->user_model->edit($where,$loginTime);

//            $this->load->view('admin/index');
            redirect('admin/admin/index');
//            echo  'login';
        }
    }

    //权限组
    public function role(){
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        $data = $this->role_model->page('role',0,$page_num,$start_position); //limit查询的数据
        $viewData = [
            'list' => $data[0],  //这里得到从数据库中的当页数据
            'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
            'pageNum'  => $page_num //每页显示多少条

        ];

        $this->load->view('admin/user/role',$viewData);
    }

    //权限组
    public function role_add(){

    }



    //todo 注销功能
    public function logout()
    {
        $this->session->set_userdata('sys_key', null, -1);
        redirect(ADMIN_SITE_URL);
    }

    public function test()
    {
        echo 'user test测试';
    }
}